(function() {
	angular
		.module('ydApp.health', [])
		.config(function($stateProvider) {
			$stateProvider.state('health-list', {
				url: '/health-list',
				controller: "healthListCtrl",
				templateUrl: 'views/health/health-list.html'
			});
		})
		.controller("healthListCtrl", healthListCtrl);

	function healthListCtrl($scope, xhr, $cordovaInAppBrowser, $location) {
		var vm = $scope.vm = {};

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		vm.condition = {
			pageIndex: -1
		}

		// xhr.get('HealthInquiry/com/article/findByCondition', vm.condition).then(function(result) {
		// 	vm.health = result.data;
		// })

		vm.nodata = false;
		vm.health = [];

		vm.doRefresh = function() {
			vm.health = [];
			vm.condition.pageIndex = 0;
			vm.nodata = false;
			vm.load().finally(function() {
				$scope.$broadcast('scroll.refreshComplete');
			});
		}
		vm.loadMore = function() {
			vm.condition.pageIndex++;
			vm.load().then(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			});
		}
		vm.load = function() {
			return xhr.get("HealthInquiry/com/article/findByCondition", vm.condition).then(function(result) {
				if (result.data.length == 0) {
					vm.nodata = true;
				}
				vm.health = vm.health.concat(result.data);
			})
		}
		vm.toDetail = function(url) {
			$location.url(url);
		}

		var options = {
			location: 'no',
			clearcache: 'no',
			toolbar: 'yes'
		};

		$scope.open = function(link) {
			$cordovaInAppBrowser.open(link, '_blank', options);
		}
	}
})();